<?php
	$item = $this->item; 
	$frm_sendmail = $this->form_sendmailtoposter;
?>
<div class="block">
	<div class="title">
		<span>Nội dung tin nhắn</span>
	</div>
	<div class="content">
		<div class="wrap-content" style="padding: 10px;">
			<div style="font-weight: bold; font-size: 13px;">
				<?php echo $item->tieu_de; ?><br/>
				<span style="font-size: 11px; color: #c0c0c0;"><?php echo $item->ngay_gui; ?></span>
			</div>
			<div class="view-info">
				<div style="font-size: 13px;">
					<dl class="accordion">
						<?php echo $item->noi_dung; ?>
					</dl>
				</div>
				<?php 
					$forward = '<li><a class="forward" href="#sendMailModal" data-toggle="modal">Trả lời</a></li>';
					if ($item->nguoi_gui == 0) {
						$forward = '';
				?><br/>
				<b style="color: red;">Chú ý</b>: <br/>
				<ul class="note">
					<li>Do người gửi không đăng ký tài khoản trên website, nên không có chức năng trả lời mail này.</li>
					<li>Bạn muốn gửi mail phản hồi hãy đăng nhập vào mail bạn đã đăng ký với website để phản hồi.</li>
				</ul>
				<?php } ?> 
				<div class="toolbar">
					<ul>
						<li><a class="back" href="#" onclick="history.go(-1)">Quay lại</a></li>
						<?php echo $forward; ?>
					</ul>
				</div>
			</div>
			<!-- SEND MAIL CHO NGUOI DANG MODAL -->
			<div id="sendMailModal" class="modal hide fade" tabindex="-1"
				role="dialog" aria-labelledby="sendMailModalLabel" aria-hidden="true">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-hidden="true">×</button>
					<h3 id="sendMailModalLabel">Trả lời tin nhắn</h3>
				</div>
				<div class="modal-body">
					<div id="sendmailalert" class="alert"></div>
					<form class="form-horizontal">
						<?php 
							echo $frm_sendmail->id;
							echo $frm_sendmail->loai_bai_dang;
							echo $frm_sendmail->id_bai_dang;
							echo $frm_sendmail->id_nguoi_dang;
							echo $frm_sendmail->email_nguoi_dang;
						?>
						<div class="control-group">
							<label class="control-label" for="nguoi_gui">Tên của bạn</label>
							<div class="controls">
							<?php echo $frm_sendmail->nguoi_gui;?>
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" for="email_nguoi_gui">Email của bạn</label>
							<div class="controls">
							<?php echo $frm_sendmail->email_nguoi_gui;?>
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" for="subject">Tiêu đề (Subject)</label>
							<div class="controls">
							<?php echo $frm_sendmail->subject;?>
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" for="message">Thông điệp (Message)</label>
							<div class="controls">
								<?php echo $frm_sendmail->message;?>
							</div>
						</div>
					</form>
				</div>
				<div class="modal-footer">
					<button class="btn" data-dismiss="modal" aria-hidden="true">Bỏ qua</button>
					<button id="btnSendMail" class="btn btn-primary">Gửi</button>
				</div>
			</div>
			<br class="clear" />
		</div>
	</div>
	<script type="text/javascript">
		//Catch keydown event of the enter key
		$('#sendMailModal').keydown(function (e){
			if(e.keyCode == 13){
				sendForFriendProcess(e);
			}
		});
		//Catch click event of the send mail button
		$('#btnSendMail').click(function(e) {
			sendMailProcess(e);
		});
		
		$('#sendmail').click(function() {
			$('#sendmailalert').hide();
		});
		
		//Show the modal event processing
		$('#sendMailModal').on('show', function () {
			$(this).find('.alert').hide();
			$(this).find("textarea").each(function() {
				var inputid = $(this).attr('id');
				$("#"+inputid).val("");
		    });
		});
		
		//Process send mail function
		function sendMailProcess(event) {
			var url = '/ajax/replymail';
			var $this = $(this);
			var postData = $("#sendMailModal form").serializeObject();
			postData.format = "json";
			var isValid = true;
			$('#sendMailModal').find("input").each(function() {
				var inputid = $(this).attr('id');
				isValid = isValid&&doValidation('sendMailModal', inputid, '/ajax/validatesendmail', 'validate');
		    });
			if(isValid){
				$("#btnSendMail").attr("disabled", "disabled");
				$.ajax({
		            type: 'POST',
		            url: url,
		            async: true,
		            cache: true,
		            data: postData,
		            dataType: postData.format,
		            success: function(data){
		            	if(data.result) {
	                		$('#sendmailalert').html(data.message);
	                		$('#sendmailalert').show();
		            		setTimeout(function() {
								$('#sendMailModal').modal('hide'); 
							}, 1000);
		            	} else {
		            		if(data.message){
		                		$('#sendmailalert').html(data.message);
		                		$('#sendmailalert').show();
		                    }
		                }
		            }
		        });
				$("#btnSendMail").removeAttr("disabled");
			}
		}

		$(function(){
			(function($) {
				var allPanels = $('.accordion > dd').hide();
				$('.accordion > dd.active').show();
	
				$('.accordion > dt > a').click(function() {
					$this = $(this);
					$target =  $this.parent().next();
	
					if(!$target.hasClass('active')) {
						$target.addClass('active').slideDown();
					} else {
						$target.removeClass('active').slideUp();
					}
					 
				    return false;
				});
			})(jQuery);
		});
	</script>
	<br class="clear" />
</div>